import axios from 'axios';

// 接口返回的类型
export interface Res {
  data: {
    Code: number;
    Message: string;
    Data: any;
    Count?: number;
    [key: string]: any;
  };
}

// 初始化实例
const service = axios.create({
  baseURL: '',
  timeout: 6000,
  headers: {
    'Content-Type': 'application/json',
  },
});

// 请求拦截器
service.interceptors.request.use(config => {
  // 请求头添加毫秒级时间戳
  const timeStamp = new Date().getTime();
  config.headers['time-stamp'] = timeStamp;
  // 添加平台id
  config.headers['PT'] = 2;
  // 添加渠道id
  config.headers['MT'] = 1;
  // 添加UT
  if (config.method?.toLocaleLowerCase() === 'get' && config.params?.UT) {
    config.headers['UT'] = config.params?.UT;
    delete config.params.UT;
  } else if (config.method?.toLocaleLowerCase() === 'post' && config.data?.UT) {
    config.headers['UT'] = config.data?.UT;
    delete config.data.UT;
  }
  // 添加语言
  if (config.method?.toLocaleLowerCase() === 'get' && config.params?.language) {
    config.headers['Language'] = config.params?.language;
    delete config.params.language;
  } else if (
    config.method?.toLocaleLowerCase() === 'post' &&
    config.data?.language
  ) {
    config.headers['Language'] = config.data?.language;
    delete config.data.language;
  }
  return config;
});

// 登录过期封装
const loginExpire = (msg?: string) => {
  console.error(msg);
};

// 响应拦截器
service.interceptors.response.use(
  response => {
    const {status, data} = response;
    const code = data?.Code;
    const msg = data?.Message;
    // 如果请求状态是200并且返回的数据中code是210为请求成功
    if (status === 200) {
      if (code === 210 || (data?.bannerList && data?.groupList)) {
        return Promise.resolve(data);
      }
      //参数异常;
      if ([-2, -3, -4].includes(code)) {
        //
      } else if (code === -999) {
        // 权限校验失败
        // loginExpire(msg);
      } else if ([-1000, -1001].includes(code)) {
        // token 失效
        // loginExpire(msg);
      }
    }
    return Promise.reject(response);
  },
  (error: {response: {status: any}; message: string | string[]}) => {
    // axios设置的timeout超过后接口没有返回就会走这里
    if (!error.response && error?.message.indexOf('timeout') !== -1) {
      // tip('网络请求超时');
      return Promise.reject(error);
    }
    // 这里只会捕获http请求失败的状态码3x、4x、5x等
    switch (error?.response?.status) {
      // 未登录
      case 401:
        loginExpire();
        break;
      // token 过期
      case 403:
        loginExpire();
        break;
      // 网络请求不存在
      case 404:
        // tip('网络请求不存在');
        break;
      // 服务端错误
      case 501:
      case 502:
      case 503:
      case 504:
        // tip('服务端错误');
        break;
      default:
        break;
    }
    return Promise.reject(error);
  },
);

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
function get(url: string, params?: Record<string, any>) {
  return new Promise((resolve, reject) => {
    service
      .get(url, {params})
      .then((res: Res) => resolve(res))
      .catch((err: Res) => reject(err));
  });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
function post(url: string, params: Record<string, any>) {
  return new Promise((resolve, reject) => {
    service
      .post(url, params)
      .then((res: Res) => resolve(res))
      .catch((err: Res) => reject(err));
  });
}

/**
 * postForm方法，对应postForm请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
function postForm(url: string, params: Record<string, any>) {
  // TODO 文件上传需要对参数进行formData 转化，目前还没用到，还未实现
  return new Promise((resolve, reject) => {
    service
      .postForm(url, params)
      .then((res: Res) => resolve(res))
      .catch((err: Res) => reject(err));
  });
}

export {get, post, postForm};
